
package com.hmall.cart.listener;

import cn.hutool.json.JSONUtil;
import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.Year;
import java.util.List;
import java.util.Map;

import static com.fasterxml.jackson.databind.type.LogicalType.Collection;

/**
 * @create: 张曦文
 * 日期 :2025/11/6 21:21
 * 功能 :
 **/

@Component
@RequiredArgsConstructor
public class CartListener {
    private final ICartService iCartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue"),
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC),
            key="RoutingKey"
    ))
    public void  car(String msg){

        Map<String, Object> map = JSONUtil.toBean(msg, Map.class);
        Long userId = Long.valueOf(map.get("userId").toString());
        List<Long> itemIds = (List<Long>) map.get("itemIds");


        UserContext.setUser(userId);
        iCartService.removeByItemIds(itemIds);

        System.out.println("接收到消息后清理指定用户的购物车中的指定商品");
    }
}

